/*
 * @date 2020年03月31日 18:30
 */
package org.jsola.hr.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jsola.approval.entity.UserApproInfoDO;
import org.jsola.approval.vo.ApproModuleContext;
import org.jsola.approval.vo.UserApproAllInfoVO;
import org.jsola.approval.vo.UserApproInfoListVO;
import org.jsola.common.StrKit;
import org.jsola.core.IPojo;
import org.jsola.json.JsonKit;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author june
 */
public class ApproHelper {

    /**
     * 获取审批里字段的值
     * @param approModuleContext 审批上下文
     * @param formId 字段名
     * @return 字段的值
     */
    public static String getFormValueByFormId(ApproModuleContext approModuleContext, String formId) {
        List<UserApproInfoDO> userApproInfoList = approModuleContext.getUserApproInfoList();
        List<UserApproInfoDO> userApproInfoNoFormList = approModuleContext.getUserApproInfoNoFormList();
        List<UserApproInfoDO> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userApproInfoList)) {
            list.addAll(userApproInfoList);
        }
        if (!CollectionUtils.isEmpty(userApproInfoNoFormList)) {
            list.addAll(userApproInfoNoFormList);
        }
        for (UserApproInfoDO userApproInfoDO : list) {
            String props = userApproInfoDO.getProps();
            if (JsonKit.isValidJson(props)) {
                JSONObject jsonObject = JSON.parseObject(props);
                if (formId.equals(jsonObject.getString("model"))) {
                    return userApproInfoDO.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 获取审批表单的值，转成pojo
     * @param approModuleContext 审批上下文
     * @param clazz pojo类型
     * @param <T> pojo
     * @return pojo
     */
    public static <T extends IPojo> T getFormValueToPojo(ApproModuleContext approModuleContext, Class<T> clazz) {
        List<UserApproInfoDO> userApproInfoList = approModuleContext.getUserApproInfoList();
        List<UserApproInfoDO> userApproInfoNoFormList = approModuleContext.getUserApproInfoNoFormList();
        List<UserApproInfoDO> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userApproInfoList)) {
            list.addAll(userApproInfoList);
        }
        if (!CollectionUtils.isEmpty(userApproInfoNoFormList)) {
            list.addAll(userApproInfoNoFormList);
        }
        Map<String, String> map = new HashMap<>(20);
        for (UserApproInfoDO userApproInfoDO : list) {
            String props = userApproInfoDO.getProps();
            if (userApproInfoDO.getIsForm() && JsonKit.isValidJson(props)) {
                JSONObject jsonObject = JSON.parseObject(props);
                String model = jsonObject.getString("model");
                if (StrKit.isEmpty(model)) {
                    continue;
                }
                map.put(model, userApproInfoDO.getValue());
            }
            if (!userApproInfoDO.getIsForm()) {
                map.put(userApproInfoDO.getFormId(), userApproInfoDO.getValue());
            }
        }
        return JsonKit.parseObject(JsonKit.toJSONString(map), clazz);
    }


    /**
     * 获取审批表单的值，转成pojo
     * @param userApproAllInfoVO 审批表单信息
     * @param clazz pojo类型
     * @param <T> pojo
     * @return pojo
     */
    public static <T extends IPojo> T getFormValueToPojo(UserApproAllInfoVO userApproAllInfoVO, Class<T> clazz) {
        List<UserApproInfoListVO> userApproInfoList = userApproAllInfoVO.getUserApproInfoList();
        List<UserApproInfoListVO> userApproInfoNoFormList = userApproAllInfoVO.getUserApproInfoNoFormList();
        List<UserApproInfoListVO> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userApproInfoList)) {
            list.addAll(userApproInfoList);
        }
        if (!CollectionUtils.isEmpty(userApproInfoNoFormList)) {
            list.addAll(userApproInfoNoFormList);
        }
        Map<String, String> map = new HashMap<>(20);
        for (UserApproInfoListVO userApproInfoDO : list) {
            String props = userApproInfoDO.getProps();
            if (userApproInfoDO.getIsForm() && JsonKit.isValidJson(props)) {
                JSONObject jsonObject = JSON.parseObject(props);
                String model = jsonObject.getString("model");
                if (StrKit.isEmpty(model)) {
                    continue;
                }
                map.put(model, userApproInfoDO.getValue());
            }
            if (!userApproInfoDO.getIsForm()) {
                map.put(userApproInfoDO.getFormId(), userApproInfoDO.getValue());
            }
        }
        return JsonKit.parseObject(JsonKit.toJSONString(map), clazz);
    }
}
